/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Exerciseset;
import me.zhengjie.modules.system.domain.QuestionNum;
import me.zhengjie.modules.system.domain.QuicklyQuestion;
import me.zhengjie.modules.system.service.ExercisesetService;
import me.zhengjie.modules.system.domain.vo.ExercisesetQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;

import me.zhengjie.utils.R;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author shitou
* @date 2024-04-21
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "习题集（试卷）管理")
@RequestMapping("/api/exerciseset")
public class ExercisesetController {

    private final ExercisesetService exercisesetService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('exerciseset:list')")
    public void exportExerciseset(HttpServletResponse response, ExercisesetQueryCriteria criteria) throws IOException {
        exercisesetService.download(exercisesetService.queryAll(criteria), response);
    }
    @GetMapping("/queryExerciseset")
    @Log("查询习题集（试卷）")
    @ApiOperation("查询习题集（试卷）")
    public ResponseEntity<PageResult<Exerciseset>> queryExerciseset2(ExercisesetQueryCriteria criteria, Page<Object> page){

        criteria.setState(null);

        return new ResponseEntity<>(exercisesetService.queryAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping
    @Log("查询习题集（试卷）")
    @ApiOperation("查询习题集（试卷）")
    public ResponseEntity<PageResult<Exerciseset>> queryExerciseset(ExercisesetQueryCriteria criteria, Page<Object> page){
        if(criteria.getState() == null){
            criteria.setState(0);
        }

        return new ResponseEntity<>(exercisesetService.queryAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping("/queryQuicklyExerciseset")
    @Log("查询习题集（试卷）")
    @ApiOperation("查询习题集（试卷）")
//    @PreAuthorize("@el.check('exerciseset:list')")
    public ResponseEntity<PageResult<Exerciseset>> queryQuicklyExerciseset(ExercisesetQueryCriteria criteria, Page<Object> page){
        criteria.setState(1);
        return new ResponseEntity<>(exercisesetService.queryAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping("/getQuestionNum")
    @Log("获取试题数量")
    @ApiOperation("获取试题数量")
    public ResponseEntity<QuestionNum> getQuestionNum(QuicklyQuestion criteria){

        return new ResponseEntity<>(exercisesetService.getQuestionNum(criteria),HttpStatus.OK);
    }


    @PostMapping("/queryQuicklyQuestion")
    @Log("快速创建试题")
    @ApiOperation("快速创建试题")
    public R<Object> queryQuicklyQuestion(@Validated @RequestBody QuicklyQuestion criteria){

        Integer state = exercisesetService.queryQuicklyQuestion(criteria);
        if (state == -1){
            return R.error("该题库单选试题数量不足");
        } else if(state == -2){
            return R.error("该题库多选试题数量不足");
        } else if(state == -3){
            return R.error("该题库判断试题数量不足");
        } else if(state == -4){
            return R.error("该题库填空试题数量不足");
        } else if(state == -5){
            return  R.error("该题库简答试题数量不足");
        }
        return R.success("创建成功");
    }


    @PostMapping
    @Log("新增习题集（试卷）")
    @ApiOperation("新增习题集（试卷）")
//    @PreAuthorize("@el.check('exerciseset:add')")
    public ResponseEntity<Object> createExerciseset(@Validated @RequestBody Exerciseset resources){
        exercisesetService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改习题集（试卷）")
    @ApiOperation("修改习题集（试卷）")
//    @PreAuthorize("@el.check('exerciseset:edit')")
    public ResponseEntity<Object> updateExerciseset(@Validated @RequestBody Exerciseset resources){
        exercisesetService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除习题集（试卷）")
    @ApiOperation("删除习题集（试卷）")
//    @PreAuthorize("@el.check('exerciseset:del')")
    public ResponseEntity<Object> deleteExerciseset(@RequestBody List<Integer> ids) {
        exercisesetService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}